package com.parkingspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parkingspace.dao.ComplainMapper;
import com.parkingspace.dao.UserMapper;
import com.parkingspace.entity.Complain;
import com.parkingspace.entity.User;
import com.parkingspace.service.IComplainService;
import com.parkingspace.util.JwtUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 投诉表 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@Service
public class ComplainServiceImpl extends ServiceImpl<ComplainMapper, Complain> implements IComplainService {

	@Resource
	private UserMapper userMapper;

	@Resource
	private ComplainMapper complainMapper;

	@Override
	public Boolean insertComplain(String token, Complain complain) throws Exception {
		String username = JwtUtils.getUsername(token);
		if (username == null) {
			throw new RuntimeException("请先进行登录!");
		}
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("uname",username);
		User user = userMapper.selectOne(queryWrapper);
		if (complain == null) {
			throw new RuntimeException("参数非法!");
		}
		complain.setUid(user.getUid());
		complain.setUname(username);
		boolean b = complainMapper.insert(complain) == 1;
		if (!b) {
			throw new RuntimeException("创建投诉异常!");
		}
		return b;

	}

	@Override
	public List<Complain> findByOrderId (Long ordereid) throws Exception {
		if (ordereid == null) {
			throw new RuntimeException("非法参数!");
		}
		QueryWrapper<Complain> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("orderid", ordereid);
		List<Complain> complains = complainMapper.selectList(queryWrapper);
		if (complains.size() == 0) {
			throw new RuntimeException("该订单没有被投诉!");
		}
		return complains;
	}
}
